//
//  ChooseRank.m
//  LvjFarm
//
//  Created by 张仁昊 on 16/4/20.
//  Copyright © 2016年 _____ZXHY_____. All rights reserved.
//

#import "ChooseRank.h"
#import "GoodsStandardModel.h"

#define leftX 14

@interface ChooseRank ()
@property (strong, nonatomic) GoodsStandardModel * standardModel;

@end

@implementation ChooseRank

- (instancetype)initWithTitle:(id)title titleArr:(NSArray *)titleArr andFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        self.title = title;

        self.rankArray = [NSArray arrayWithArray:titleArr];
        
        [self rankView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame data:(id)data {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        if ([[data class] isSubclassOfClass:[GoodsStandardModel class]]) {
            self.standardModel = data;
            [self rankView];
        }
    }
    return self;
}

- (void)rankView {
    
    self.packView = [[UIView alloc] initWithFrame:self.frame];
    self.packView.y = 0;
    UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(leftX, 5, ScreenWidth, 25)];
    titleLB.text = self.standardModel.value;//规格名称//self.title;

    titleLB.font = APPFONT(14);
    titleLB.textColor = normalColor;
    [self.packView addSubview:titleLB];
    
    self.btnView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLB.frame), ScreenWidth, 40)];
    [self.packView addSubview:self.btnView];
    
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    for (int i = 0; i < self.standardModel.children.count; i++) {
        GoodsStandardModel * model = self.standardModel.children[i];
        NSString *btnName = model.value;//子规格名称//self.rankArray[i];
        
        StandardButton *btn = [StandardButton buttonWithType:UIButtonTypeCustom];
        [btn setTitleColor:normalColor forState:UIControlStateNormal];
        btn.standardModel = model;

        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:btnName forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.cornerRadius = 2;
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor = borderNormalColor;
        btn.layer.borderWidth = 0.5;
        
        NSDictionary *dict = [NSDictionary dictionaryWithObject:APPFONT(14) forKey:NSFontAttributeName];
        CGSize btnSize = [btnName sizeWithAttributes:dict];
        
        CGFloat w = btnSize.width;
        if (w < 10) {
            w = 10;
        }
        btn.width = w + 26;
        btn.height = btnSize.height + 21;
        
        if (i == 0) {
            btn.x = leftX;
            btnWidth += CGRectGetMaxX(btn.frame);
        } else {
            btnWidth += CGRectGetMaxX(btn.frame)+10;
            if (btnWidth > ScreenWidth) {
                count++;
                btn.x = leftX;
                btnWidth = CGRectGetMaxX(btn.frame);
            } else {
                btn.x += btnWidth - btn.width;
            }
        }
        btn.y += count * (btn.height+10)+10;
        
        viewHeight = CGRectGetMaxY(btn.frame)+10;
        
        [self.btnView addSubview:btn];
        
        btn.tag = 1000 +i;
    }
    self.btnView.height = viewHeight;
    self.packView.height = self.btnView.height+CGRectGetMaxY(titleLB.frame);

    self.height = self.packView.height;
    
    [self addSubview:self.packView];
}

- (void)btnClick:(UIButton *)btn {
    
    if (![self.selectBtn isEqual:btn]) {
        [self.selectBtn setTitleColor:normalColor forState:UIControlStateNormal];
        self.selectBtn.layer.borderColor = borderNormalColor;
        self.selectBtn.selected = NO;
    } else {
        [btn setTitleColor:Color175030 forState:UIControlStateNormal];
        btn.layer.borderColor = Color175030.CGColor;
    }
    [btn setTitleColor:Color175030 forState:UIControlStateNormal];
    btn.layer.borderColor = Color175030.CGColor;
    btn.selected = YES;
    
    self.selectBtn = btn;
    [self.standardModel.children enumerateObjectsUsingBlock:^(GoodsStandardModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.value isEqualToString:btn.titleLabel.text]) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(selectStandardBtn:goodsStandardModel:headerValue:)]) {
                [self.delegate selectStandardBtn:btn goodsStandardModel:obj headerValue:self.standardModel.value];
            }
            * stop = YES;
        }
    }];
}

@end

@implementation StandardButton


@end





